<!--
 * @Autor: dingxiaolin
 * @Date: 2021-07-21 16:23:40
 * @LastEditors: dingxiaolin
 * @LastEditTime: 2021-07-30 15:32:08
-->
<template>
    <div class="account-settings-info-view">
        <a-row :gutter="16">
            <a-col :md="24" :lg="16">
                <a-form ref="formRef" :model="form" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <a-form-item label="昵称" name="nickName" :rules="[{ required: true, message: '请输入昵称', trigger: 'change' }]">
                        <a-input
                            :maxLength="50"
                            :disabled="$store.getters['user/userInfo'].nickName ? true : false"
                            placeholder="给自己起个昵称吧"
                            v-model:value="form.nickName"
                        />
                    </a-form-item>
                    <a-form-item label="生日" name="birthday" :rules="[{ required: true, message: '请选择生日', trigger: 'blur' }]">
                        <a-date-picker
                            placeholder="请选择生日"
                            style="width: 100%"
                            v-model:value="form.birthday"
                            valueFormat="YYYY-MM-DD"
                            :disabled-date="disabledDate"
                        />
                    </a-form-item>
                    <a-form-item label="性别" name="sex" :rules="[{ required: true, message: '请选择性别', trigger: 'change' }]">
                        <a-radio-group v-model:value="form.sex">
                            <a-radio v-for="(item, index) in sexlist" :key="index" :value="item.code">{{ item.value }}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                    <a-form-item label="手机" name="phone" :rules="formRules.phone">
                        <a-input placeholder="请输入手机号" :maxLength="13" v-model:value="form.phone" />
                    </a-form-item>
                    <a-form-item label="电话" name="tel" :rules="[{ required: true, message: '请输入电话', trigger: 'change' }]">
                        <a-input placeholder="请输入电话" :maxLength="13" v-model:value="form.tel" />
                    </a-form-item>
                    <a-form-item label="电子邮件" name="email" :rules="formRules.email">
                        <a-input placeholder="请输入电子邮件地址" :maxLength="45" v-model:value="form.email" />
                    </a-form-item>
                    <a-form-item :wrapper-col="{ span: 18, offset: 6 }">
                        <a-button type="primary" @click="submitUserInfo">更新基本信息</a-button>
                    </a-form-item>
                </a-form>
            </a-col>
            <!-- <a-col :md="24" :lg="8">
                <div class="ant-upload-preview" @click="$refs.modalRef.edit()">
                    <CloudUploadOutlined class="upload-icon" />
                    <div class="mask">
                        <PlusOutlined />
                    </div>
                    <img :src="avatarUrl" class="avatar" />
                </div>
            </a-col> -->
        </a-row>
        <!-- 头像上传组件 -->
        <!-- <avatarCropper ref="modalRef" v-model:url="avatarUrl"></avatarCropper> -->
    </div>
</template>

<script lang='ts'>
import { defineComponent, reactive, toRefs, ref, computed, watch, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from '@/store'
import { message } from 'ant-design-vue'
import { sysUserUpdateInfo, getUserInfo } from '@/api/system/user'
import { formRules } from '@/utils/validator'
import dayjs from 'dayjs'
import { getSrc, syncObj } from '@/utils/common'
import avatarCropper from './avatarCropper.vue'

export default defineComponent({
    name: 'baseSettings',
    components: {
        avatarCropper
    },
    props: {},
    emits: [],
    setup(props, ctx) {
        const router = useRouter()
        const store = useStore()
        const formRef = ref<any>(null)
        const modalRef = ref<any>(null)
        const state = reactive({
            labelCol: {
                xs: { span: 24 },
                sm: { span: 6 }
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 18 }
            },
            sexlist: [],
            userInfo: {},
            avatarUrl: getSrc('avatar.jpg'),
            form: {
                id: '',
                nickName: '',
                birthday: '',
                sex: '',
                phone: '',
                tel: '',
                email: '',
                avatar: ''
            }
        })

        onMounted(() => {
            store.dispatch('enums/getEnum', 'sex').then((res) => {
                state.sexlist = res
            })
            getUserInfo().then((res) => {
                syncObj(state.form, res)
                state.form.sex = state.form.sex.toString()
                state.form.birthday = dayjs(state.form.birthday).format('YYYY-MM-DD')
            })
        })

        const submitUserInfo = () => {
            formRef.value
                .validate()
                .then(() => {
                    // if (state.avatarUrl.includes('http')) state.form.avatar = state.avatarUrl
                    sysUserUpdateInfo(state.form).then((res) => {
                        message.success('个人信息更新成功')
                        getUserInfo().then((res) => {
                            syncObj(state.form, res)
                            state.form.sex = state.form.sex.toString()
                            state.form.birthday = dayjs(state.form.birthday).format('YYYY-MM-DD')
                        })
                    })
                })
                .catch((err) => {})
        }
        /**
         * 禁用日期
         */
        const disabledDate = (current) => {
            return current && current > dayjs().endOf('day') //当天之前的不可选，包括当天
        }
        return {
            ...toRefs(state),
            formRef,
            modalRef,
            formRules,
            disabledDate,
            submitUserInfo
        }
    }
})
</script>

<style lang='less' scoped>
.avatar-upload-wrapper {
    height: 200px;
    width: 100%;
}

.ant-upload-preview {
    position: relative;
    margin: 0 auto;
    width: 180px;
    height: 180px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;

    .upload-icon {
        position: absolute;
        top: 0;
        right: 10px;
        font-size: 1.4rem;
        padding: 0.5rem;
        background: rgba(222, 221, 221, 0.4);
        border-radius: 50%;
        border: 1px solid rgba(0, 0, 0, 0.2);
    }
    .mask {
        opacity: 0;
        position: absolute;
        background: rgba(0, 0, 0, 0.2);
        cursor: pointer;
        transition: opacity 0.4s;

        &:hover {
            opacity: 1;
        }

        .anticon-plus {
            font-size: 2rem;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-left: -1rem;
            margin-top: -1rem;
            color: #d6d6d6;
        }
    }

    img,
    .mask {
        width: 100%;
        max-width: 180px;
        height: 100%;
        border-radius: 50%;
        overflow: hidden;
    }
}
</style>
